<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检索" prop="search">
        <el-input v-model="queryParams.search" placeholder="请输入检索信息" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" clearable>
          <el-option label="正常" :value="0"></el-option>
          <el-option label="停用" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="queryParams.sex" clearable>
          <el-option label="男" :value="0"></el-option>
          <el-option label="女" :value="1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="登录时间" prop="loginDate">
        <el-date-picker clearable v-model="queryParams.loginDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="登录时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['store:member:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['store:member:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['store:member:remove']">删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="index" width="50" label="编号"/>
      <el-table-column label="昵称" align="center" prop="nickName"/>
      <el-table-column label="账号" align="center" prop="memberName" width="150px"/>
      <el-table-column label="手机" align="center" prop="phone" width="110px">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.phone != null">
            {{ scope.row.phone }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>
            暂无
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="归属地" align="center" prop="phoneAddress">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.phoneAddress != null">
            {{ scope.row.phoneAddress }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>
            暂无
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" prop="email" width="150px">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.email != null">{{ scope.row.email }}</el-tag>
          <el-tag effect="dark" type="info" v-else>暂无邮箱</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1"
                     @change="handleStatusChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-image :src="scope.row.avatar" fit="cover" style=" width:50; height:50;">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <el-tag effect="dark" :type="scope.row.sex == '0' ? 'danger' : 'info'" v-if="scope.row.sex != null">
            {{ scope.row.sex == '0' ? '男' : '女' }}
          </el-tag>
          <el-tag v-else effect="dark" type="danger">不详</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="登录IP" align="center" prop="loginIp" width="100px">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.loginIp != null">{{ scope.row.loginIp }}</el-tag>
          <el-tag v-else effect="dark" type="info">暂无Ip</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="IP地址" align="center" prop="ipAddress">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.ipAddress != null">{{ scope.row.ipAddress }}</el-tag>
          <el-tag v-else effect="dark" type="info">暂无地址</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="登录类型" align="center" prop="loginType" width="100">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.loginType != null">{{ scope.row.loginType }}</el-tag>
          <el-tag v-else effect="dark" type="info">暂无三方</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="登录时间" align="center" prop="loginDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['store:member:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['store:member:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改会员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称"/>
        </el-form-item>

        <el-form-item el-form-item label="账号" prop="memberName" v-if="form.memberName != null">
          <el-input placeholder="账号" v-model="form.memberName" :disabled="true">
          </el-input>
        </el-form-item>

        <el-form-item label="手机" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机"/>
        </el-form-item>
        <el-form-item label="归属地" prop="phoneAddress" v-if="form.phoneAddress != null">
          <el-input placeholder="归属地" v-model="form.phoneAddress" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch v-model="form.status" active-value="0" inactive-value="1">
          </el-switch>
        </el-form-item>


        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"/>
        </el-form-item>

        <el-form-item label="性别" prop="valueType">
          <el-radio-group v-model="form.sex">
            <el-radio :label="'0'">男</el-radio>
            <el-radio :label="'1'" checked>女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-upload class="upload-demo" v-model="form.icon" action="/dev-api//store/member/upload"
                     :on-success="handleSuccess" :file-list="fileList" list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item el-form-item label="IP" prop="loginIp" v-if="form.loginIp != null">
          <el-input placeholder="IP" v-model="form.loginIp" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item el-form-item label="IP地址" prop="ipAddress" v-if="form.ipAddress != null">
          <el-input placeholder="IP" v-model="form.ipAddress" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item el-form-item label="三方登录" prop="openId" v-if="form.openId != null">
          <el-input placeholder="IP" v-model="form.loginType" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {changeMemberStatus, listMember, getMember, delMember, addMember, updateMember} from "@/api/store/member";

export default {
  name: "Member",
  data() {
    return {
      fileList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        sex: null,
        loginDate: null
      },
      // 表单参数
      form: {
        sex: '1',
        status: '0',
      },
      // 表单校验
      rules: {
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "修改时间不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 上传成功 回调函数
    handleSuccess(response, file, fileList) {
      console.log(response);
      var data = {
        name: response.name,
        url: response.file
      }
      this.fileList.push(data)
      this.form.avatar = response.file
    },
    // 修改会员状态
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + row.nickName + '"吗？').then(function () {
        return changeMemberStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 查询会员列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.fileList = []
      this.form =
        {
          sex: '1',
          status: '0',
        };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        search: null,
        status: null,
        sex: null,
        loginDate: null
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        var data = {
          name: response.data.avatar,
          url: response.data.avatar
        }
        this.fileList.push(data)
        this.form = response.data;
        this.open = true;
        this.title = "修改会员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addMember(this.form).then(response => {
            this.$modal.msgSuccess(response.msg);
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员编号为"' + ids + '"的数据项？').then(function () {
        return delMember(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

  }
};
</script>
<style lang="scss" scoped>
.el-image {
  border-radius: 5px;
  background-color: #ebeef5;
  box-shadow: 0 0 5px 1px #ccc;

  ::v-deep .el-image__inner {
    transition: all 0.3s;
    cursor: pointer;

    &:hover {
      transform: scale(1.2);
    }
  }

  ::v-deep .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    color: #909399;
    font-size: 30px;
  }
}
</style>
